<template>
  <div class="remote-control-container gradient-bg">
    <!-- 顶部导航栏 -->
    <div class="nav-bar glass">
      <button class="back-btn mobile-optimized" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </button>
      <h1 class="page-title">遥控控制</h1>
      <div class="connection-status">
        <span :class="['status-indicator', carStore.statusClass]"></span>
        <span class="status-text">{{ carStore.statusText }}</span>
      </div>
    </div>

    <div class="main-content landscape-container">
      <div class="landscape-left">
        <!-- 按钮控制区域 -->
        <div class="control-section card glass">
          <h3 class="section-title">
            <el-icon><Grid /></el-icon>
            按钮控制
          </h3>
          <div class="button-grid">
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('LEFT_ROTATE')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('LEFT_ROTATE')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><RefreshLeft /></el-icon>
              <span>左转</span>
            </button>
            
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('FRONT')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('FRONT')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Top /></el-icon>
              <span>前进</span>
            </button>
            
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('RIGHT_ROTATE')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('RIGHT_ROTATE')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><RefreshRight /></el-icon>
              <span>右转</span>
            </button>
            
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('LEFT')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('LEFT')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Back /></el-icon>
              <span>左移</span>
            </button>
            
            <button 
              class="control-btn stop-btn mobile-optimized" 
              @touchstart="handleButtonPress('STOP')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('STOP')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><VideoPause /></el-icon>
              <span>停止</span>
            </button>
            
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('RIGHT')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('RIGHT')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Right /></el-icon>
              <span>右移</span>
            </button>
            
            <div class="empty-cell"></div>
            
            <button 
              class="control-btn direction-btn mobile-optimized" 
              @touchstart="handleButtonPress('AFTER')"
              @touchend="handleButtonRelease"
              @mousedown="handleButtonPress('AFTER')"
              @mouseup="handleButtonRelease"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Bottom /></el-icon>
              <span>后退</span>
            </button>
            
            <div class="empty-cell"></div>
          </div>
        </div>

        <!-- 功能控制区域 -->
        <div class="control-section card glass">
          <h3 class="section-title">
            <el-icon><Tools /></el-icon>
            功能控制
          </h3>
          <div class="function-buttons button-group">
            <button 
              class="btn-secondary function-btn mobile-optimized" 
              @click="takePhoto"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Camera /></el-icon>
              拍照
            </button>
            
            <button 
              class="btn-secondary function-btn mobile-optimized" 
              @click="toggleRecording"
              :disabled="!carStore.isConnected"
            >
              <el-icon><VideoCamera v-if="!carStore.isRecording" /><VideoPlay v-else /></el-icon>
              {{ carStore.isRecording ? '停止录像' : '开始录像' }}
            </button>
            
            <button 
              class="btn-secondary function-btn mobile-optimized" 
              @click="toggleTracking"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Aim /></el-icon>
              {{ carStore.isTracking ? '停止循迹' : '开始循迹' }}
            </button>
          </div>
        </div>
      </div>

      <div class="landscape-right">
        <!-- 摇杆控制区域 -->
        <div class="control-section card glass">
          <h3 class="section-title">
            <el-icon><Mouse /></el-icon>
            摇杆控制
          </h3>
          <div class="joystick-container">
            <JoystickControl 
              @change="handleJoystickChange"
              @start="handleJoystickStart"
              @end="handleJoystickEnd"
            />
          </div>
        </div>

        <!-- 状态显示区域 -->
        <div class="control-section card glass">
          <h3 class="section-title">
            <el-icon><Monitor /></el-icon>
            状态信息
          </h3>
          <div class="status-info">
            <div class="status-item">
              <span class="status-label">连接状态:</span>
              <span :class="['status-value', carStore.statusClass]">
                {{ carStore.statusText }}
              </span>
            </div>
            <div class="status-item">
              <span class="status-label">当前速度:</span>
              <span class="status-value">
                X: {{ carStore.currentSpeed.x }}, Y: {{ carStore.currentSpeed.y }}
              </span>
            </div>
            <div class="status-item">
              <span class="status-label">录像状态:</span>
              <span class="status-value">
                {{ carStore.isRecording ? '录制中' : '未录制' }}
              </span>
            </div>
            <div class="status-item">
              <span class="status-label">循迹状态:</span>
              <span class="status-value">
                {{ carStore.isTracking ? '循迹中' : '未循迹' }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCarStore } from '../stores/car'
import { commonApi } from '../utils/api'
import JoystickControl from '../components/JoystickControl.vue'
import { 
  ArrowLeft, 
  RefreshLeft, 
  Top, 
  RefreshRight, 
  Back, 
  VideoPause, 
  Right, 
  Bottom,
  Camera,
  VideoCamera,
  VideoPlay,
  Aim,
  Grid,
  Tools,
  Mouse,
  Monitor
} from '@element-plus/icons-vue'

const router = useRouter()
const carStore = useCarStore()

// 页面加载时获取连接状态
onMounted(async () => {
  await carStore.getConnectionStatus()
})

// 返回上一页
const goBack = () => {
  router.back()
}

// 按钮按下
const handleButtonPress = async (direction) => {
  if (!carStore.isConnected) return
  
  const result = await carStore.buttonControl(direction)
  if (!result.success) {
    commonApi.showError(result.message)
  }
}

// 按钮释放
const handleButtonRelease = async () => {
  if (!carStore.isConnected) return
  
  const result = await carStore.buttonControl('STOP')
  if (!result.success) {
    commonApi.showError(result.message)
  }
}

// 摇杆变化
const handleJoystickChange = async (data) => {
  if (!carStore.isConnected) return
  
  const result = await carStore.joystickControl(data.x, data.y)
  if (!result.success) {
    commonApi.showError(result.message)
  }
}

// 摇杆开始
const handleJoystickStart = () => {
  // 可以添加摇杆开始时的逻辑
}

// 摇杆结束
const handleJoystickEnd = async () => {
  if (!carStore.isConnected) return
  
  const result = await carStore.joystickControl(0, 0)
  if (!result.success) {
    commonApi.showError(result.message)
  }
}

// 拍照
const takePhoto = async () => {
  const result = await carStore.cameraControl('photo')
  if (result.success) {
    commonApi.showSuccess('拍照成功')
  } else {
    commonApi.showError(result.message)
  }
}

// 切换录像状态
const toggleRecording = async () => {
  const action = carStore.isRecording ? 'record_stop' : 'record_start'
  const result = await carStore.cameraControl(action)
  if (result.success) {
    commonApi.showSuccess(carStore.isRecording ? '停止录像成功' : '开始录像成功')
  } else {
    commonApi.showError(result.message)
  }
}

// 切换循迹状态
const toggleTracking = async () => {
  const result = await carStore.trackingControl(!carStore.isTracking)
  if (result.success) {
    commonApi.showSuccess(carStore.isTracking ? '开始循迹成功' : '停止循迹成功')
  } else {
    commonApi.showError(result.message)
  }
}
</script>

<style scoped>
.remote-control-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  position: relative;
  overflow-x: hidden;
}

/* 导航栏 */
.nav-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 16px 20px;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 16px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(10px);
  color: var(--text-primary);
}

.back-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: translateY(-2px);
  box-shadow: var(--shadow-medium);
}

.page-title {
  flex: 1;
  font-size: 20px;
  font-weight: 700;
  color: white;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.connection-status {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.status-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

/* 主要内容 */
.main-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.control-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 24px;
  box-shadow: var(--shadow-medium);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.control-section:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-heavy);
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 20px 0;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.button-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  max-width: 320px;
  margin: 0 auto;
}

.control-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 20px 16px;
  border: none;
  border-radius: 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
  touch-action: manipulation;
  min-height: 80px;
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.direction-btn {
  background: var(--gradient-primary);
  color: white;
  box-shadow: var(--shadow-medium);
}

.direction-btn:hover:not(:disabled) {
  transform: translateY(-4px) scale(1.05);
  box-shadow: var(--shadow-heavy);
}

.direction-btn:active:not(:disabled) {
  transform: translateY(-2px) scale(1.02);
  box-shadow: var(--shadow-medium);
}

.stop-btn {
  background: linear-gradient(135deg, var(--error-color), #f87171);
  color: white;
  box-shadow: var(--shadow-medium);
}

.stop-btn:hover:not(:disabled) {
  transform: translateY(-4px) scale(1.05);
  box-shadow: var(--shadow-heavy);
}

.empty-cell {
  grid-column: span 1;
}

.function-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.function-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px 20px;
  font-size: 14px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: var(--gradient-secondary);
  border: 2px solid var(--primary-color);
  color: var(--primary-color);
  font-weight: 600;
}

.function-btn:hover:not(:disabled) {
  background: var(--primary-color);
  color: white;
  transform: translateY(-2px);
  box-shadow: var(--shadow-medium);
}

.function-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.joystick-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.status-info {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.status-label {
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 500;
}

.status-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-bar {
    padding: 12px 16px;
  }
  
  .page-title {
    font-size: 18px;
  }
  
  .main-content {
    padding: 16px;
    gap: 16px;
  }
  
  .control-section {
    padding: 20px;
    border-radius: 18px;
  }
  
  .button-grid {
    gap: 12px;
    max-width: 280px;
  }
  
  .control-btn {
    padding: 16px 12px;
    font-size: 12px;
    min-height: 70px;
  }
  
  .function-buttons {
    gap: 8px;
  }
  
  .function-btn {
    padding: 14px 16px;
    font-size: 13px;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) {
  .main-content {
    flex-direction: row;
    gap: 20px;
  }
  
  .landscape-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 20px;
  }
  
  .landscape-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 20px;
  }
  
  .button-grid {
    max-width: 250px;
  }
  
  .function-buttons {
    flex-direction: row;
    flex-wrap: wrap;
  }
  
  .function-btn {
    flex: 1;
    min-width: 120px;
  }
}
</style> 